<?php
/**
 * Minhlog Domain_Mapper 友情链接类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_DataMapper
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Link.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_DataMapper_Link extends Domain_DataMapper_Abstract
{
    protected $_tableClassName = 'Domain_Table_Link';

    protected $_dataMapper = array (
            'linkId'       => 'link_id',
            'typeId'       => 'type_id',
            'siteName'     => 'site_name',
            'siteDesc'     => 'site_desc',
            'siteUrl'      => 'site_url',
            'displayOrder' => 'display_order'
    );

    /**
     * 查询全部友情链接
     * @return object|void
     */
    public function findAllLink()
    {
        return $this->findAll($this->findAllLinkStatement());
    }

    /**
     * 根据链接类型查询友情链接
     * @param Zend_Db_Table_Rowset $linkType
     * @return object|void
     */
    public function findLinkByType(Zend_Db_Table_Rowset $linkType)
    {
        if($linkType->count()){
            $links = array();
            foreach($linkType as $type){
                $linkSet = $type->findDependentRowset('Domain_Table_Link','Type',$this->findLinkByTypeStatement($type->type_id));
                if($linkSet->count()){
                    foreach($linkSet as $link){
                        $typeLink = $this->mapper($link);
                        $links[$type->type_id][] = $typeLink;
                    }
                }
            }
            $resultSet = $links;
        }else{
            $resultSet = null;
        }
        return $resultSet;
    }

    /**
     * 封装根据链接类型查询友情链接SQL对象
     * @return Zend_Db_Table_Select
     */
    private function findLinkByTypeStatement($typeId)
    {
        $db = $this->getTable()->getAdapter();
        $select = $this->getTable()->select();
        $select->where($db->quoteInto('type_id = ?', $typeId))
                ->order('display_order')
                ->order('link_id DESC');
        return $select;
    }

    /**
     * 封装查询全部友情链接SQL对象
     * @return Zend_Db_Table_Select
     */
    private function findAllLinkStatement()
    {
        $select = $this->getTable()->select();
        $select->order('link_id DESC');
        return $select;
    }
}